package com.LC.Service.Impl;

import com.LC.DAO.RoleDAO;
import com.LC.DAO.UserDAO;
import com.LC.Entity.*;
import com.LC.Service.UserService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Set;

@Service(interfaceClass = UserService.class)
@Transactional
public class UserServiceImpl implements UserService {
  
    @Autowired
    UserDAO userDAO;
    
    @Autowired
    RoleDAO roleDAO;
    /**
     * 根据用户名查询数据库获取用户信息和关联的角色信息，同时需要查询角色关联的权限信息
     * @param username
     * @return
     */
    @Override
    public User findByUsername(String username) {
        
        User userByUsername = userDAO.findByUsername(username);
        Set<Role> roleSetByUserId = userDAO.RoleSetByUserId(userByUsername.getId());
        if (roleSetByUserId.size() >0) { 
            for (Role role : roleSetByUserId) {
              Set<Permission> permissionSetByRoleId = userDAO.PermissionSetByRoleId(role.getId());
              role.setPermissions(permissionSetByRoleId);
            }
        }
        if (roleSetByUserId.size()>0){ 
            userByUsername.setRoles(roleSetByUserId);
        }
        return userByUsername;  
    }

    @Override
    public PageResult getUserPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        //使用mybatis分页助手
        PageHelper.startPage(currentPage,pageSize);
        Page<User> userPage = userDAO.userPage(queryString);
        return new PageResult(userPage.getTotal(),userPage.getResult());
    }


    @Override
    public List<Role> getRoleList() {
       return roleDAO.findAll();
    }

    @Override
    public void AddUser(Integer[] RoleIds, User addUser) {
        User byUsername = userDAO.findByUsername(addUser.getUsername());
        if (byUsername != null){
            throw new RuntimeException();
        }else {
            userDAO.AddUser(addUser);
            int userId = addUser.getId();
            if (RoleIds != null && RoleIds.length>0){
                for (Integer roleId : RoleIds) {
                    userDAO.addTot_user_role(userId, roleId);
                }
            }
        }
    }

    @Override
    public User GetUserById(int id) {
        return userDAO.findUserById(id);
    }
  
    @Override
    public List<Integer> RoleNumByUId(int id) {
        return userDAO.RoleNumByUId(id);
    }

    @Override
    public void EditUser(Integer[] RoleIds,User UpdateUser) {
        try {
            userDAO.updateUser(UpdateUser);
            if (RoleIds != null && RoleIds.length > 0){
                for (Integer roleId : RoleIds) {
                    userDAO.deleteFrom_t_user_role(UpdateUser.getId());
                    userDAO.addTot_user_role(UpdateUser.getId(), roleId);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void DeleteUserById(int id) {
        try {
            userDAO.deleteFrom_t_user_role(id);
            userDAO.deleteUserById(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
